package servlet;

import entity.News;
import entity.Page;
import entity.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import service.NewsDaoService;
import service.UserDaoService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/showNewsNav")
public class showNewsNav extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        NewsDaoService nds = new NewsDaoService();
        //每页的数据量
//        String pageSize = request.getParameter("pageSize");
        String pageSize = "8";
        //前端页码
        String pageNum = request.getParameter("pageNum");
        //查询类型
//        String type = request.getParameter("type");
        String type = "1";
        //总数据量
        Long totalRecord = nds.newsCount(Integer.parseInt(type));
        //总页数
        //总页数 = (总记录数 + 每页显示记录数 - 1) / 每页显示记录数
        int totalPage = (int) ((totalRecord +Integer.parseInt(pageSize) - 1) / Integer.parseInt(pageSize));
        //当前页
        int truePageNum = (Integer.parseInt(pageNum)-1)*Integer.parseInt(pageSize);
        //获取当前页的数据
        List<News> list = nds.selectNewsType(Integer.parseInt(type), Integer.parseInt(pageSize),truePageNum,1 );
        session.setAttribute("list",list);
        Page page = new Page();
        page.setTotalPage(totalPage);
        page.setPageNumber(Integer.parseInt(pageNum));
        page.setTotalRecord(totalRecord);
        page.setIndex(truePageNum);
        session.setAttribute("page",page);
        response.sendRedirect("./WinterTourism/material/NewsShow.jsp");
    }
}
